package com.whccb.apps.office.document.event.receive;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.HistoryTaskInstance;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.TaskInstance;
import com.actionsoft.bpms.org.cache.UserCache;
import com.actionsoft.bpms.org.model.UserModel;
import com.actionsoft.bpms.util.ConsolePrinter;
import com.actionsoft.sdk.local.SDK;

public class ReceiveFileCheckSubTaskCompleteAndUpdateUserColorAfterFormLoadEvent
        extends ExecuteListener {
    public String getDescription() {
        return "查询收文办理子流程办理情况，已办结的显示蓝色。";
    }

    public String getProvider() {
        return "ActionSoft";
    }

    public String getVersion() {
        return "AWS 6.0";
    }

    public void execute(ProcessExecutionContext pec) throws Exception {
        // 获取表单所有的标签
        Map<String, Object> macroLibraries = pec
                .getParameterOfMap(ListenerConst.FORM_EVENT_PARAM_TAGS);
        String bindId = pec.getProcessInstance().getId();
        String oldDoLerdeeHtml = macroLibraries.get("LEADERMAN") + "";
        if (oldDoLerdeeHtml != null && oldDoLerdeeHtml.contains("<label")) {
            BO bo = SDK.getBOAPI().getByProcess("BO_WHBK_OD_RECEIVE",
                    pec.getProcessInstance().getId());
            if (bo != null) {
                String dolerdername = bo.getString("LEADERMAN");
                String dolerderid = bo.getString("LEADERMANID");
                ConsolePrinter.info("dolerderid----" + dolerderid);
                if (StringUtils.isNotBlank(dolerderid)) {
                    String[] dolerderids = dolerderid.split(",");
                    if (dolerderids.length > 0) {
                        String html = "<input type='hidden' name='LEADERMAN' id='LEADERMAN' value='"
                                + dolerdername + "'/>";
                        // 获取代办任务节点
                        List<TaskInstance> aTasklist = SDK.getTaskQueryAPI()
                                .activityDefId(
                                        "obj_c7496d875a3000011bfb7a507d201a12")
                                .processInstId(bindId).desc().list();
                        // 获取已办任务节点
                        List<HistoryTaskInstance> hTasklist = SDK
                                .getHistoryTaskQueryAPI()
                                .activityDefId(
                                        "obj_c7496d875a3000011bfb7a507d201a12")
                                .processInstId(bindId).desc().list();
                        aTasklist.addAll(hTasklist); // 将领导批示节点的代办已办任务加起来
                        ConsolePrinter
                                .info("atasklist--------" + aTasklist.size());
                        if (aTasklist != null && !aTasklist.isEmpty()
                                && aTasklist.size() > 0) {
                            for (String lerderid : dolerderids) {
                                for (TaskInstance ti : aTasklist) {
                                    if (lerderid.trim()
                                            .equals(ti.getTarget())) { // 寻找任务
                                        UserModel user = UserCache
                                                .getModel(ti.getTarget());
                                        if (ti.isHistoryTask()) {// 判断任务是否已经办理
                                            String htmlLabel = "<label id='LEADERMAN_Readonly' style='color:blue'>"
                                                    + (user != null
                                                            ? (user.getUserName()
                                                                    + ",")
                                                            : "")
                                                    + "</label>";
                                            html = html + htmlLabel;
                                        } else {
                                            String htmlLabel = "<label id='LEADERMAN_Readonly' style='color:red'>"
                                                    + (user != null
                                                            ? (user.getUserName()
                                                                    + ",")
                                                            : "")
                                                    + "</label>";
                                            html = html + htmlLabel;
                                        }
                                        break;
                                    }
                                }
                            }
                            html = html.substring(0, html.lastIndexOf(","))
                                    + html.substring(html.lastIndexOf(",") + 1);
                            macroLibraries.put("LEADERMAN", html);
                        }
                    }
                }
            }
        }
        String oldDoUserNameHtml = macroLibraries.get("DOUSERNAME") + "";
        if (oldDoUserNameHtml != null && oldDoUserNameHtml.contains("<label")) {
            BO bo = SDK.getBOAPI().getByProcess("BO_WHBK_OD_RECEIVE",
                    pec.getProcessInstance().getId());
            if (bo != null) {
                String doUserNameStr = bo.getString("DOUSERNAME");
                String doUserIdStr = bo.getString("DOUSERID");
                if (StringUtils.isNotEmpty(doUserIdStr)) {
                    String[] doUserIds = doUserIdStr.split(",");
                    if (doUserIds != null && doUserIds.length > 0) {
                        String html = "<input type='hidden' name='DOUSERNAME' id='DOUSERNAME' value='"
                                + doUserNameStr + "'/>";
                        List<ProcessInstance> subProcessList = SDK
                                .getProcessAPI().query()
                                .parentProcessInstId(
                                        pec.getProcessInstance().getId())
                                .list();
                        for (String doUserId : doUserIds) {
                            if (StringUtils.isNotEmpty(doUserId)) {
                                if (subProcessList != null
                                        && !subProcessList.isEmpty()
                                        && subProcessList.size() > 0) {
                                    for (ProcessInstance subProcess : subProcessList) {
                                        if (subProcess != null) {
                                            if (doUserId.trim()
                                                    .equals(subProcess
                                                            .getCreateUser()
                                                            .trim())) {
                                                UserModel user = UserCache
                                                        .getModel(doUserId
                                                                .trim());
                                                if (!subProcess.isEnd()) {
                                                    String htmlLabel = "<label id='DOUSERNAME_Readonly' style='color:red'>"
                                                            + (user != null
                                                                    ? (user.getUserName()
                                                                            + ",")
                                                                    : "")
                                                            + "</label>";
                                                    html = html + htmlLabel;
                                                } else {
                                                    String htmlLabel = "<label id='DOUSERNAME_Readonly' style='color:blue'>"
                                                            + (user != null
                                                                    ? (user.getUserName()
                                                                            + ",")
                                                                    : "")
                                                            + "</label>";
                                                    html = html + htmlLabel;
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        html = html.substring(0, html.lastIndexOf(","))
                                + html.substring(html.lastIndexOf(",") + 1);
                        macroLibraries.put("DOUSERNAME", html);
                    }
                }

            }
        }

    }

    public static void main(String[] args) {
        String string = "1234567,89";
        System.out.println(string.lastIndexOf(","));
        System.out.println(string.substring(0, string.lastIndexOf(","))
                + string.substring(string.lastIndexOf(",") + 1));
    }
}
